import React, { Component } from "react";

export default class App extends Component {
  constructor(props) {
    super(props);
    /* 1. 创建一个引用 */
    this.inputRef = React.createRef();
  }
  handleFocus = () => {
    this.inputRef.current.focus();
  };
  render() {
    return (
      <div>
        {/* 2. 绑定引用到元素上 */}
        <input type="text" ref={this.inputRef} />
        <button onClick={this.handleFocus}>自动聚焦</button>
      </div>
    );
  }
}
